﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraSplashScreen;

namespace zmMoniter
{
    public partial class frmLoading : SplashScreen
    {
        private mainForm main = null;
        
        public frmLoading(mainForm frm)
        {
            InitializeComponent();
            main = frm;
        }
        public void setMsg(string sMsg)
        {
            lab_msg.Text = sMsg;
        }
        #region Overrides

        public override void ProcessCommand(Enum cmd, object arg)
        {
            base.ProcessCommand(cmd, arg);
        }

        #endregion

        public enum SplashScreenCommand
        {
        }
        private void Login()
        {
            
            PublicDataCls db = new PublicDataCls();
            string sSQLState=db.OpenConn();
                
            if (sSQLState != "")
            {
                lab_msg.Text = sSQLState;
                lab_msg.ForeColor = Color.Red;
                return;
            }
           
            string sSQL = "select 用户.*," +
            "权限.名称 AS 权限名称 " +
            "FROM 用户 LEFT OUTER JOIN " +
            "权限 ON 用户.权限 = 权限.编号 " +
            "WHERE 用户.登陆名='" + txtEdt_UserName.Text + "'";

            DataTable dt = db.ReturnDataTable(sSQL);
            db.CloseConn();
            if (dt == null)
            {
                lab_msg.Text = "数据库连接失败！";
                lab_msg.ForeColor = Color.Red;
                return;       
            }
            int iCount = dt.Rows.Count;
            if (iCount == 0)
            {
                lab_msg.Text = "警告！未找到用户名为 " + txtEdt_UserName.Text + " 的用户！";
                lab_msg.ForeColor = Color.Red;
                return;
            }
            DataRow dataRow = dt.Rows[0];
            string strUserID = dataRow["ID"].ToString().Trim();

            string strUserName = dataRow["用户名"].ToString().Trim();
            string strPsw = dataRow["密码"].ToString().Trim();
            string strPrvName = dataRow["权限名称"].ToString().Trim();
            string strPrv = dataRow["权限"].ToString().Trim();
            
            string sMD5Psw = MD5Hashing.HashString(txtEdt_Password.Text);
            
            if (strPsw == sMD5Psw)
            {
                if (upDateLoginfo(strUserID) > 0)
                {
                    //登陆成功
                    main.sUserID = strUserID;
                    main.sUserName = strUserName;
                    main.sPsw = strPsw;
                    main.sPrvName = strPrvName;
                    main.sPrv = strPrv;
                    main.upDateCaption();
                    lab_msg.Text = "登陆成功！正在载入系统...";
                    
                    lab_msg.ForeColor = Color.Black;
                    main.startTimer(true);
                    if (strPrv == "1")
                        main.setPurview(true);
                    else
                        main.setPurview(false);
                }
                else
                {
                    lab_msg.Text = "警告！用户登陆信息更新失败！";
                    lab_msg.ForeColor = Color.Red;
                    return;
                }
            }
            else
            {
                lab_msg.Text = "警告！密码错误！";
                lab_msg.ForeColor = Color.Red;
                return;
            }
        }
        public void Logout()
        {
            DateTime now = System.DateTime.Now;
            PublicDataCls db = new PublicDataCls();
            db.OpenConn();

            string sSQL = "UPDATE 用户 SET 是否在线=0,离线时间=to_date('" + now.ToString("yyyy/MM/dd hh:mm:ss") + "','YYYY/MM/DD HH24:MI:SS')" + " WHERE ID=" + main.sUserID;
            db.ExecuteSQL(sSQL);
            db.CloseConn();
        }
        private void btn_Login_Click(object sender, EventArgs e)
        {

            Login();
        }
        private int getUserPort()
        {
            return main.iMonPort;
        }
        private void btn_Exit_Click(object sender, EventArgs e)
        {
            
        }
        private int upDateLoginfo(string sUID)
        {
            string sIP = GetIP();
            DateTime now = System.DateTime.Now;
            PublicDataCls db = new PublicDataCls();
            db.OpenConn();

            string sSQL = "UPDATE 用户 SET IP='" + sIP + "',是否在线= 1,离线时间=null,登陆时间=to_date('" + now.ToString() + "','YYYY/MM/DD HH24:MI:SS')" + "WHERE ID=" + sUID;
            
            int iResult = 0;
            iResult=db.ExecuteSQL(sSQL);
            db.CloseConn();
            return iResult;
        }
        protected string GetIP()   //获取本地IP
        {
            System.Net.IPHostEntry ipHost = System.Net.Dns.Resolve(System.Net.Dns.GetHostName());
            System.Net.IPAddress ipAddr = ipHost.AddressList[0];
            return ipAddr.ToString();
        }

        private void txtEdt_UserName_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
                txtEdt_Password.Focus();
           
        }

        private void txtEdt_Password_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
                Login();
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            this.Hide();
            main.showSetting();
            
            //main.showDbSertting(1);
        }
    }
}